package vn.edu.thptduongbachmai.common;

import hapax.TemplateDataDictionary;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import vn.edu.thptduongbachmai.model.pojo.Entity;

/**
 *
 * @author tiennv4
 */
public class Paging<P extends Entity> {
    
    private int _curPage;
    private long _totalEntries;
    private int _pageSize;
    private int _totalPage;
    private List<P> _records;
    private String[] fieldNames;
    private TemplateDataDictionary _dic;
    private String _page_ref;
    
    public Paging() {
        _curPage = 1;
        _totalEntries = 0;
        _pageSize = 1;
    }
    
    public Paging(int curPage, int pageSize, long totalRecord, TemplateDataDictionary dic, String[] fieldNames,String page_ref) {
        this._curPage = curPage;
        this._pageSize = pageSize;
        this._totalEntries = totalRecord;
        this._dic = dic;
        this._page_ref = page_ref;
        this.fieldNames = fieldNames;
        //evaluate total page and check current page
        _totalPage = (int) (_totalEntries / _pageSize);
        if ((_totalEntries % _pageSize) > 0) {
            _totalPage += 1;
        }
        if (_curPage > _totalPage) {
            _curPage = _totalPage;
        }
        
        if (_curPage <= 0) {
            _curPage = 1;
        }
        
    }
    public void setData(List<P> data)
    {
        _records = data;
    }
    public long getNextPage() {
        long next = _curPage < _totalPage ? _curPage + 1 : _totalPage;
        return next;
    }
    
    public long getPreviousPage() {
        long prev = _curPage > 1 ? _curPage - 1 : _curPage;
        return prev;
    }
    
    public TemplateDataDictionary doPaging() {
        for (String string : fieldNames) {
            TemplateDataDictionary column_names = _dic.addSection("COLUMN_NAME");
            
            column_names.setVariable("columnname", string);
            
            
        }
        int i = 0;
        TemplateDataDictionary content = _dic.getSection("CONTENT").get(0);
        TemplateDataDictionary section = content.getSection("SECTION").get(0);
        
        for (P p : _records) {
            Map<String, Object> row = p.getFields();
            TemplateDataDictionary items = section.addSection("ITEMS");
            items.setVariable("i", String.valueOf(i));
            
            for (String string : fieldNames) {
                
                TemplateDataDictionary values = items.addSection("VALUES");
                String value = row.get(string).toString();
                values.setVariable("value", value);
                
            }
            i++;
            
        }
        section.setVariable("begin", String.valueOf(getStartPosition()));
        section.setVariable("end", String.valueOf(getEndPosition()));
        section.setVariable("total", String.valueOf(getTotalEntries()));
        //Paging item
        if (_totalPage > 1) {
            TemplateDataDictionary paging = section.addSection("SECTION_PAGING");
            
            List<Integer> pages = getPagesAvailable(5);
            paging.setVariable("page_ref", _page_ref);
            paging.setVariable("pager_previous", String.valueOf(getPreviousPage()));
            paging.setVariable("pager_next", String.valueOf(getNextPage()));
            //Create Dynamic Link
            int length = pages.size();
            for (i = 0; i < length; ++i) {
                TemplateDataDictionary pageLink = paging.addSection("PAGE");
                pageLink.setVariable("page", Integer.toString(pages.get(i)));
                if (pages.get(i) == _curPage) {
                    pageLink.setVariable("class_current", "active");
                }
                
            }
            
        }
        return _dic;
    }
    
    public int getCurrentPage() {
        return _curPage;
    }
    
    public long getPosition() {
        return _pageSize * (_curPage - 1);
    }
    
    public long getStartPosition() {
        long beginNum = _pageSize * (_curPage - 1) + 1;
        if (beginNum > _totalEntries) {
            return _totalEntries;
        }
        return beginNum;
    }
    
    public long getEndPosition() {
        if (getPosition() + _pageSize > _totalEntries) {
            return _totalEntries;
        }
        return getPosition() + _pageSize;
    }
    
    public int getPageSize() {
        return _pageSize;
    }
    
    public int getTotalPage() {
        return _totalPage;
    }
    
    public long getTotalEntries() {
        return _totalEntries;
    }
    
    public List<Integer> getPagesAvailable(int num) {
        if (num > _totalPage || num <= 0) {
            num = _totalPage;
        }
        int start = _curPage - (num / 2);
        if (start <= 0) {
            start = 1;
        }
        int end = start + num;
        if (end > _totalPage) {
            end = _totalPage;
        }
        List< Integer> ret = new ArrayList<Integer>(num);
        for (int i = start; i <= end; ++i) {
            ret.add(i);
        }
        return ret;
    }
    
    public static void main(String[] args) {
//        Paging paging = new Paging(5, 10, 101);
//        List<Integer> pageAvailable = paging.getPagesAvailable(5);
//        System.out.println(pageAvailable);
    }
}
